<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/greenwood.gitee.io/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/greenwood.gitee.io/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/greenwood.gitee.io/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/greenwood.gitee.io/images/logo.svg" color="#222">

<link rel="stylesheet" href="/greenwood.gitee.io/css/main.css">


<link rel="stylesheet" href="/greenwood.gitee.io/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"gitee.com","root":"/greenwood.gitee.io/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="本文主要带来关于区间DP的套路，算法框架。三道典型的例题为：Leetcode 312 戳气球Leetcode 486 预测赢家Leetcode 664 奇怪打印机">
<meta property="og:type" content="article">
<meta property="og:title" content="区间DP">
<meta property="og:url" content="https://gitee.com/greenwood-drgon/greenwood.gitee.io/2021/10/21/%E5%8C%BA%E9%97%B4DP/index.html">
<meta property="og:site_name" content="我看刑">
<meta property="og:description" content="本文主要带来关于区间DP的套路，算法框架。三道典型的例题为：Leetcode 312 戳气球Leetcode 486 预测赢家Leetcode 664 奇怪打印机">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/greenwood-drgon/greenwood.gitee.io/2021/10/21/%E5%8C%BA%E9%97%B4DP/image-20211022210108610.png">
<meta property="og:image" content="https://gitee.com/greenwood-drgon/greenwood.gitee.io/2021/10/21/%E5%8C%BA%E9%97%B4DP/image-20211022210610316.png">
<meta property="og:image" content="https://gitee.com/greenwood-drgon/greenwood.gitee.io/2021/10/21/%E5%8C%BA%E9%97%B4DP/image-20211022211115939.png">
<meta property="article:published_time" content="2021-10-20T19:17:34.000Z">
<meta property="article:modified_time" content="2021-10-22T13:32:32.177Z">
<meta property="article:author" content="Dybala">
<meta property="article:tag" content="leetcode">
<meta property="article:tag" content="算法">
<meta property="article:tag" content="动态规划">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/greenwood-drgon/greenwood.gitee.io/2021/10/21/%E5%8C%BA%E9%97%B4DP/image-20211022210108610.png">

<link rel="canonical" href="https://gitee.com/greenwood-drgon/greenwood.gitee.io/2021/10/21/%E5%8C%BA%E9%97%B4DP/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>区间DP | 我看刑</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/greenwood.gitee.io/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">我看刑</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/greenwood.gitee.io/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/greenwood.gitee.io/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/greenwood.gitee.io/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/greenwood.gitee.io/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-commonweal">

    <a href="/greenwood.gitee.io/404/" rel="section"><i class="fa fa-heartbeat fa-fw"></i>公益 404</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/greenwood-drgon/greenwood.gitee.io/2021/10/21/%E5%8C%BA%E9%97%B4DP/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/greenwood.gitee.io/images/avatar.gif">
      <meta itemprop="name" content="Dybala">
      <meta itemprop="description" content="日子越来越有判头了">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="我看刑">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          区间DP
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-10-21 03:17:34" itemprop="dateCreated datePublished" datetime="2021-10-21T03:17:34+08:00">2021-10-21</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-10-22 21:32:32" itemprop="dateModified" datetime="2021-10-22T21:32:32+08:00">2021-10-22</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/greenwood.gitee.io/categories/Leetcode/" itemprop="url" rel="index"><span itemprop="name">Leetcode</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>本文主要带来关于区间DP的套路，算法框架。<br>三道典型的例题为：<br><a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/burst-balloons/">Leetcode 312 戳气球</a><br><a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/predict-the-winner/">Leetcode 486 预测赢家</a><br><a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/strange-printer/">Leetcode 664 奇怪打印机</a><br><span id="more"></span><br>区间dp的主要框架如下<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//枚举长度</span></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> len=<span class="number">0</span>;len&lt;array.length;len++)&#123;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;array.length;i++)&#123;</span><br><span class="line">		<span class="comment">//枚举起点</span></span><br><span class="line">		<span class="keyword">int</span> j=i+len;<span class="comment">//终点</span></span><br><span class="line">		<span class="keyword">if</span>(j&gt;=array.length)<span class="keyword">break</span>;</span><br><span class="line">		<span class="comment">//枚举中间端点</span></span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> k=i+<span class="number">1</span>;k&lt;=j;k++)&#123;</span><br><span class="line">			f[i][j]=<span class="comment">//逻辑处理</span></span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">return</span> f[<span class="number">0</span>][array.length]</span><br></pre></td></tr></table></figure></p>
<h2 id="312-戳气球"><a href="#312-戳气球" class="headerlink" title="312 戳气球"></a>312 戳气球</h2><p><img src="image-20211022210108610.png" alt="image-20211022210108610"></p>
<p>由于碰到边界则自动计为1，为了简单起见，在原数组的两侧各加上一个1。<br>定义f(i,j)为(i,j)的最大值，不包含边界。则</p>
<script type="math/tex; mode=display">
f(i,j)=max  f(i,k)+f(k,j)+array(i)\times array(j)\times array(k)</script><p>代码如下：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">maxCoins</span><span class="params">(<span class="keyword">int</span>[] nums)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 区间dp</span></span><br><span class="line">        <span class="keyword">int</span> temp[]=<span class="keyword">new</span> <span class="keyword">int</span>[nums.length+<span class="number">2</span>];</span><br><span class="line">        temp[<span class="number">0</span>]=<span class="number">1</span>;</span><br><span class="line">        temp[temp.length-<span class="number">1</span>]=<span class="number">1</span>;</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=nums.length;i++)&#123;</span><br><span class="line">            temp[i]=nums[i-<span class="number">1</span>];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">int</span> [][]f=<span class="keyword">new</span> <span class="keyword">int</span>[temp.length][temp.length];</span><br><span class="line">        <span class="comment">// f(i,j)表示(i,j)内的最大值，开区间</span></span><br><span class="line">        <span class="keyword">int</span> len=<span class="number">2</span>;</span><br><span class="line">        <span class="keyword">int</span> []book =<span class="keyword">new</span> <span class="keyword">int</span>[temp.length];</span><br><span class="line">        <span class="keyword">for</span>(;len&lt;temp.length;len++)&#123;</span><br><span class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;temp.length;i++)&#123;</span><br><span class="line">                <span class="keyword">int</span> j=i+len;</span><br><span class="line">                <span class="keyword">if</span>(j&gt;=temp.length)&#123;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">for</span>(<span class="keyword">int</span> k=i+<span class="number">1</span>;k&lt;j;k++)&#123;</span><br><span class="line">                    f[i][j]=Math.max(f[i][j],f[i][k]+f[k][j]+temp[k]*temp[i]*temp[j]);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> f[<span class="number">0</span>][temp.length-<span class="number">1</span>];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="486-预测赢家"><a href="#486-预测赢家" class="headerlink" title="486 预测赢家"></a>486 预测赢家</h2><p><img src="image-20211022210610316.png" alt="image-20211022210610316"></p>
<p>这题的难点在于将问题转换成区间DP。<br>两个玩家每次都需要拿走边上两个数字，既要保证自己拿的大，又要尽可能限制对手拿的数字。<br>定义f(i,j)为先手玩家在[i,j]上所取得的最大值，因此有转移方程：</p>
<script type="math/tex; mode=display">
f(i,j)=max(array[i]-f[i+1][j],array[j]-f[i][j-1])</script><p>前一个表示如果选择左边边界，则当前获得array(i)，下一局相当于对手先手，所以减去对手先手拿到的最大值。<br>后一个分析同上。</p>
<p>代码如下<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">PredictTheWinner</span><span class="params">(<span class="keyword">int</span>[] nums)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> n=nums.length;</span><br><span class="line">        <span class="keyword">int</span> [][]f=<span class="keyword">new</span> <span class="keyword">int</span>[n+<span class="number">1</span>][n+<span class="number">1</span>];</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> len=<span class="number">0</span>;len&lt;n;len++)&#123;</span><br><span class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;n;i++)&#123;</span><br><span class="line">                <span class="keyword">int</span> j=i+len;</span><br><span class="line">                <span class="keyword">if</span>(j&gt;=n)<span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">if</span>(j-<span class="number">1</span>&lt;=<span class="number">0</span>)f[i][j]=nums[j];</span><br><span class="line">                <span class="keyword">else</span>&#123;</span><br><span class="line">                    f[i][j]=Math.max(nums[j]-f[i][j-<span class="number">1</span>],nums[i]-f[i+<span class="number">1</span>][j]);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> f[<span class="number">0</span>][n-<span class="number">1</span>]&gt;=<span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="664-奇怪的打印机"><a href="#664-奇怪的打印机" class="headerlink" title="664 奇怪的打印机"></a>664 奇怪的打印机</h2><p><img src="image-20211022211115939.png" alt="image-20211022211115939"></p>
<h3 id="分析："><a href="#分析：" class="headerlink" title="分析："></a>分析：</h3><h4 id="情况1：单个字符"><a href="#情况1：单个字符" class="headerlink" title="情况1：单个字符"></a>情况1：单个字符</h4><p>在这种情况下，只需要打印一次即可</p>
<h4 id="情况2：两个不一样字符"><a href="#情况2：两个不一样字符" class="headerlink" title="情况2：两个不一样字符"></a>情况2：两个不一样字符</h4><p>比如ab，需要打印两次</p>
<h4 id="情况3：多个字符，但是首尾一样"><a href="#情况3：多个字符，但是首尾一样" class="headerlink" title="情况3：多个字符，但是首尾一样"></a>情况3：多个字符，但是首尾一样</h4><p>可以在第一次打印的时候就将第一个和最后一个打印好，因此约等于最后一个不存在</p>
<h4 id="情况4：多个字符，首尾不一样"><a href="#情况4：多个字符，首尾不一样" class="headerlink" title="情况4：多个字符，首尾不一样"></a>情况4：多个字符，首尾不一样</h4><p>定义f(i,j)为i到j闭区间的打印次数<br>f(i,j)=min f(i,k-1)+f(k,j)<br>比如说abac，关于最后一个字符c，可以组合成a和bac，或者ab和ac，或者aba和c，去最小即可</p>
<p>代码如下<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">strangePrinter</span><span class="params">(String s)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">char</span> []str=s.toCharArray();</span><br><span class="line">        <span class="keyword">int</span> [][]f=<span class="keyword">new</span> <span class="keyword">int</span>[str.length+<span class="number">1</span>][str.length+<span class="number">1</span>];</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> len=<span class="number">0</span>;len&lt;str.length;len++)&#123;</span><br><span class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;str.length;i++)&#123;</span><br><span class="line">                <span class="keyword">int</span> j=i+len;</span><br><span class="line">                f[i][j]=Integer.MAX_VALUE;</span><br><span class="line">                <span class="keyword">if</span>(j&gt;=str.length)<span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">if</span>(i==j)&#123;</span><br><span class="line">                    f[i][j]=<span class="number">1</span>;</span><br><span class="line">                    <span class="keyword">continue</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">if</span>(str[i]==str[j])&#123;</span><br><span class="line">                    f[i][j]=f[i][j-<span class="number">1</span>];</span><br><span class="line">                &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                    <span class="keyword">for</span>(<span class="keyword">int</span> k=i+<span class="number">1</span>;k&lt;=j;k++)&#123;</span><br><span class="line">                        f[i][j]=Math.min(f[i][j],f[i][k-<span class="number">1</span>]+f[k][j]);</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> f[<span class="number">0</span>][str.length-<span class="number">1</span>];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/greenwood.gitee.io/tags/leetcode/" rel="tag"># leetcode</a>
              <a href="/greenwood.gitee.io/tags/%E7%AE%97%E6%B3%95/" rel="tag"># 算法</a>
              <a href="/greenwood.gitee.io/tags/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92/" rel="tag"># 动态规划</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/greenwood.gitee.io/2021/10/19/leetcode940/" rel="prev" title="leetcode940">
      <i class="fa fa-chevron-left"></i> leetcode940
    </a></div>
      <div class="post-nav-item"></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#312-%E6%88%B3%E6%B0%94%E7%90%83"><span class="nav-number">1.</span> <span class="nav-text">312 戳气球</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#486-%E9%A2%84%E6%B5%8B%E8%B5%A2%E5%AE%B6"><span class="nav-number">2.</span> <span class="nav-text">486 预测赢家</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#664-%E5%A5%87%E6%80%AA%E7%9A%84%E6%89%93%E5%8D%B0%E6%9C%BA"><span class="nav-number">3.</span> <span class="nav-text">664 奇怪的打印机</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%86%E6%9E%90%EF%BC%9A"><span class="nav-number">3.1.</span> <span class="nav-text">分析：</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%83%85%E5%86%B51%EF%BC%9A%E5%8D%95%E4%B8%AA%E5%AD%97%E7%AC%A6"><span class="nav-number">3.1.1.</span> <span class="nav-text">情况1：单个字符</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%83%85%E5%86%B52%EF%BC%9A%E4%B8%A4%E4%B8%AA%E4%B8%8D%E4%B8%80%E6%A0%B7%E5%AD%97%E7%AC%A6"><span class="nav-number">3.1.2.</span> <span class="nav-text">情况2：两个不一样字符</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%83%85%E5%86%B53%EF%BC%9A%E5%A4%9A%E4%B8%AA%E5%AD%97%E7%AC%A6%EF%BC%8C%E4%BD%86%E6%98%AF%E9%A6%96%E5%B0%BE%E4%B8%80%E6%A0%B7"><span class="nav-number">3.1.3.</span> <span class="nav-text">情况3：多个字符，但是首尾一样</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%83%85%E5%86%B54%EF%BC%9A%E5%A4%9A%E4%B8%AA%E5%AD%97%E7%AC%A6%EF%BC%8C%E9%A6%96%E5%B0%BE%E4%B8%8D%E4%B8%80%E6%A0%B7"><span class="nav-number">3.1.4.</span> <span class="nav-text">情况4：多个字符，首尾不一样</span></a></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Dybala</p>
  <div class="site-description" itemprop="description">日子越来越有判头了</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/greenwood.gitee.io/archives/">
        
          <span class="site-state-item-count">22</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/greenwood.gitee.io/categories/">
          
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/greenwood.gitee.io/tags/">
          
        <span class="site-state-item-count">22</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Dybala</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/greenwood.gitee.io/lib/anime.min.js"></script>
  <script src="/greenwood.gitee.io/lib/velocity/velocity.min.js"></script>
  <script src="/greenwood.gitee.io/lib/velocity/velocity.ui.min.js"></script>

<script src="/greenwood.gitee.io/js/utils.js"></script>

<script src="/greenwood.gitee.io/js/motion.js"></script>


<script src="/greenwood.gitee.io/js/schemes/pisces.js"></script>


<script src="/greenwood.gitee.io/js/next-boot.js"></script>




  















  

  
      

<script>
  if (typeof MathJax === 'undefined') {
    window.MathJax = {
      loader: {
          load: ['[tex]/mhchem'],
        source: {
          '[tex]/amsCd': '[tex]/amscd',
          '[tex]/AMScd': '[tex]/amscd'
        }
      },
      tex: {
        inlineMath: {'[+]': [['$', '$']]},
          packages: {'[+]': ['mhchem']},
        tags: 'ams'
      },
      options: {
        renderActions: {
          findScript: [10, doc => {
            document.querySelectorAll('script[type^="math/tex"]').forEach(node => {
              const display = !!node.type.match(/; *mode=display/);
              const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
              const text = document.createTextNode('');
              node.parentNode.replaceChild(text, node);
              math.start = {node: text, delim: '', n: 0};
              math.end = {node: text, delim: '', n: 0};
              doc.math.push(math);
            });
          }, '', false],
          insertedScript: [200, () => {
            document.querySelectorAll('mjx-container').forEach(node => {
              let target = node.parentNode;
              if (target.nodeName.toLowerCase() === 'li') {
                target.parentNode.classList.add('has-jax');
              }
            });
          }, '', false]
        }
      }
    };
    (function () {
      var script = document.createElement('script');
      script.src = '//cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js';
      script.defer = true;
      document.head.appendChild(script);
    })();
  } else {
    MathJax.startup.document.state(0);
    MathJax.texReset();
    MathJax.typeset();
  }
</script>

    

  

</body>
</html>
